<meta title="文件管理" args="parentId:0"/>
<div class="p">
    <div>
        <ol class="breadcrumb" s="tpl" id="breadcrumb" s-data="{}">
            <li class="file-dir" data-id="0"><a href="javascript:;">根目录</a></li>
            {%$.each(path_stack, function(i){%}
            <li class="file-dir" data-id="{%=this.id%}" data-idx="{%=i%}"><a href="javascript:;">{%=this.name%}</a></li>
            {%})%}
            <li>
                <a href="javascript:;" s-click="createDir()" class="btn btn-xs btn-primary">建目录</a>
                <a href="javascript:;" class="btn btn-xs btn-warning">传文件</a>
            </li>
        </ol>
    </div>
    <div id="vfilesGrid"
         class="m"
         s='datac,nda,check'
         s-data="Smart.cleanJsonRef(S.get('/api/vfile/children/parent/{parentId}'))">
        <div class="clearfix vfile-list" s="loop" id="vfileContainer">
            <div s-loop-role="empty" class="vfile">暂无文件</div>
            <div class="vfile" s-loop-role="row" s="tpl">
                <div class="file-pic" style="background-image: url({%=this.dir ? '/images/directory.png': this.picUrl%})" class="file-pic" alt="{%=this.title%})"></div>
                <div class="file-name">{%=this.name%}</div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var baseParentId = parentId;

    var path_stack = [];

    function createDir() {
        S.popupOpen("vfile/vfile-dir-add.html", {parentId: parentId}).done(function (flag) {
            flag && $vfilesGrid.refresh();
        })
    }

    $vfileContainer.node.delegate(".vfile", 'dblclick', function(e) {
        var node = $(e.currentTarget);
        var $node = Smart.of(node);
        var data = $node.data();
        if(data.dir) {
            loadDir(data.id);
            path_stack.push(data);
            $breadcrumb.refresh();
        }
    })

    $breadcrumb.node.delegate('.file-dir', 'click', function(e) {
        var node = $(e.currentTarget);
        var idx = node.attr("data-idx");
        var id = node.attr("data-id");
        if(id == parentId) {
            return;
        }
        if(id == baseParentId) {
            loadDir(baseParentId);
            path_stack = [];
        } else {
            path_stack = path_stack.slice(0, idx + 1);
            loadDir(id);
        }
        $breadcrumb.refresh();
    })

    function loadDir(pid) {
        parentId = pid;
        $vfilesGrid.refresh();
    }

</script>